/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.component.code;

import hu.glassy.core.model.Code;
import hu.glassy.core.service.CodeService;
import java.util.List;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexander Molnár <molnar.alexander@sonrisa.hu>
 */
public class CodeSelect extends DropDownChoice<Code>
{

  @SpringBean
  CodeService codeService;

  /**
   * Constructor
   *
   * @param id         Component ID
   * @param parentCode The parent code. Only the codes under it will be
   *                   displayed
   * @param model      The model of the selected item
   */
  public CodeSelect(String id, Code parentCode, IModel<Code> model)
  {
    this(id, parentCode);
    setDefaultModel(model);
  }

  public CodeSelect(String id, Code parentCode)
  {
    super(id);

    List<Code> codeList = codeService.getChildCodes(parentCode);
    setChoices(codeList);
    setVisible(!codeList.isEmpty());

    // Set the choice renderer for this component
    setChoiceRenderer(new ChoiceRenderer<Code>("name", "id"));
  }

  @Override
  protected boolean localizeDisplayValues()
  {
    // Display localised values
    return true;
  }

}
